package controller.Operation;

import util.BookDao;
import util.NormalUserDao;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @Question Revise
 * @Date 2022/12/18 16:58
 * @Solution
 */
public class Revise {
    public static void reviseBook(JPanel Container,DefaultTableModel bookModel, JTable bookTable,JTextField bookID,
    JTextField bookNAME,JTextField bookAUTHOR,JTextField bookPRESS) {
        //检查是否有单元行被选中
        int row=bookTable.getSelectedRow();
        String bookId=bookID.getText();
        String bookName=bookNAME.getText();
        String bookAuthor=bookAUTHOR.getText();
        String bookPress=bookPRESS.getText();
        if(row==-1) {
            JOptionPane.showMessageDialog(Container, "未选择需要修改的行!");
            return;
        } else if(bookId.equals("") && bookName.equals("")
                && bookAuthor.equals("") &&bookAuthor.equals("")) {
            JOptionPane.showMessageDialog(Container, "请在左侧输入需要修改的值");
            return;
        }
        BookDao bookDao = new BookDao();
        //获取该行图书id
        int id=(int)bookModel.getValueAt(row,0);
        if(!bookId.equals("")) {
            int newId=Integer.parseInt(bookId);
            bookDao.reviseId(id,newId);
            id=newId;//保证id改变
        }
        if(!bookName.equals("")) {
            bookDao.reviseName(id,bookName);
        }
        if(!bookAuthor.equals("")) {
            bookDao.reviseAuthor(id,bookAuthor);
        }
        if(!bookPress.equals("")) {
            bookDao.revisePress(id,bookPress);
        }
        //刷新
        Show.showAllBook(bookModel);
        JOptionPane.showMessageDialog(Container, "修改成功!");
    }
    public static void reviseUser(JPanel Container,DefaultTableModel userModel, JTable userTable,JTextField userIdText,
                                  JTextField userNameText,JTextField userPasswordText) {
        //检查是否有单元行被选中
        int row=userTable.getSelectedRow();
        String userId=userIdText.getText();
        String userName=userNameText.getText();
        String userPassword=userPasswordText.getText();
        if(row==-1) {
            JOptionPane.showMessageDialog(Container, "未选择需要修改的行!");
            return;
        } else if(userId.equals("") && userName.equals("")
                && userPassword.equals("") ) {
            JOptionPane.showMessageDialog(Container, "请在左侧输入需要修改的值");
            return;
        }
        NormalUserDao normalUserDao = new NormalUserDao();
        //获取该行图书id
        int id=(int)userModel.getValueAt(row,0);
        if(!userId.equals("")) {
            int newId=Integer.parseInt(userId);
            normalUserDao.reviseId(id,newId);
            id=newId;//保证id改变
        }
        if(!userName.equals("")) {
            normalUserDao.reviseName(id,userName);
        }
        if(!userPassword.equals("")) {
            normalUserDao.revisePassword(id,userPassword);
        }
        //刷新
        Show.showAllUser(userModel);
        JOptionPane.showMessageDialog(Container, "修改成功!");
    }
}
